(function() {
	$(function() {
		upData(); //初始化数据
	})

	function upData() {
		ajaxTable({
			idNode: '#demo',
			url: 'ledger/getList',
			dataCols: [
			{fixed: true,checkbox: true,height: 180},
			{field: 'deviceCode',title: '设备编号',align: 'center',sort: true},
			{field: 'deviceName',title: '设备名称',align: 'center'},
			{field: 'deviceModel',title: '设备型号',align: 'center'},
			{field: 'deviceSpec',title: '设备规格',align: 'center'},
			{field: 'deviceInstallAddress',title: '安装地点',align: 'center'},
			{field: 'devicePositionNum',title: '设备位号',align: 'center'},
			{field: 'deviceInstallPosition',title: '安装位置',align: 'center'},
			{field: 'responsibilityDep',title: '责任部门',align: 'center'},
			{field: 'deviceStatus',title: '设备状态',align: 'center',templet:'#deviceStatus'},
			{field: 'deviceClass',title: '设备分类',align: 'center'},
			{field: 'right', title: '操作', width:170,toolbar:"#barContractUpdate"}
			],
			successFull: function(res, curr, count){
				console.log(res)
			}
		});
	};
	layui.use(['table','form'], function(){
		var table = layui.table;
		var form = layui.form;
		//列表渲染
		ajaxTable({
			idNode: '#demo',
			url:'ledger/getList',
			dataCols:[
				{fixed: true,checkbox: true,height: 180},
				{field: 'deviceCode',title: '设备编号',align: 'center',sort: true},
				{field: 'deviceName',title: '设备名称',align: 'center'},
				{field: 'deviceModel',title: '设备型号',align: 'center'},
				{field: 'deviceSpec',title: '设备规格',align: 'center'},
				{field: 'deviceInstallAddress',title: '安装地点',align: 'center'},
				{field: 'devicePositionNum',title: '设备位号',align: 'center'},
				{field: 'deviceInstallPosition',title: '安装位置',align: 'center'},
				{field: 'responsibilityDep',title: '责任部门',align: 'center'},
				{field: 'deviceStatus',title: '设备状态',align: 'center',templet:'#deviceStatus'},
				{field: 'deviceClass',title: '设备分类',align: 'center'},
				{field: 'right', title: '操作', width:170,toolbar:"#barContractUpdate"}
			],
			successFull:function(res, curr, count,table){
				table.on('toolbar(tableFilter)', function(obj){
					var checkStatus = table.checkStatus(obj.config.id);
					var data = checkStatus.data;
					if(obj.event == 'addCheckData'){
						window.location.href = './accountsAdd.html?pos=0&childPos=0'
					};
					if(obj.event == 'delCheckData') {
						if (data.length <= 0) {
							layer.msg('请勾选要删除的站点', {
								icon: 6
							});
						}else{
							var ids = [];
							$.each(data,function(i,item){
								ids.push(item.id)
							});
							$.ajax({
								url: baseUrl + "ledger/batchDel",
								type: "POST",
								data:{
									ids:ids
								},
								dataType: "json",
								success: function (data) {
									if (data.code == 200) {
										//layer.close(index);
										upData();
										layer.msg("删除成功", {icon: 6});

									} else {
										layer.msg("删除失败", {icon: 5});
									}
								}
							});
						}
					}
				})
			}
		});
		form.on('submit(search)', function(data){
			console.log(1)
            ajaxTable({
                idNode: '#demo',
                url: 'ledger/getList',
				urlParme: "&deviceCode="+data.field.jiqicode+"&deviceName="+data.field.jiqiname+"&deviceStatus="+data.field.jiqistatus+"&deviceClass="+data.field.jiqimodules+
						  "&deviceInstallAddress="+data.field.jiqiposi+"&deviceInstallPosition="+data.field.jiqiaddress+"&deviceSpec="+data.field.jiqiguige+
						  "&devicePositionNum="+data.field.jiqiweihao+"&responsibilityDep"+data.field.zerenbumen,
                dataCols: [
                    {fixed: true,checkbox: true,height: 180},
					{field: 'deviceCode',title: '设备编号',align: 'center'},
					{field: 'deviceName',title: '设备名称',align: 'center'},
					{field: 'deviceModel',title: '设备型号',align: 'center'},
					{field: 'deviceSpec',title: '设备规格',align: 'center'},
					{field: 'deviceInstallAddress',title: '安装地点',align: 'center'},
					{field: 'devicePositionNum',title: '设备位号',align: 'center'},
					{field: 'deviceInstallPosition',title: '安装位置',align: 'center'},
					{field: 'responsibilityDep',title: '责任部门',align: 'center'},
					{field: 'deviceStatus',title: '设备状态',align: 'center',templet:'#deviceStatus'},
					{field: 'deviceClass',title: '设备分类',align: 'center'},
					{field: 'right', title: '操作', width:170,toolbar:"#barContractUpdate"}
                ]
            });
        })
		//监听工具条
		table.on('tool(tableFilter)', function(obj){
			var data = obj.data;
			if(obj.event === 'detail'){
				layer.msg('ID：'+ data.id + ' 的查看操作');
			} else if(obj.event === 'del'){
				layer.confirm('确定删除吗？', function(index){
					console.log(data);
					$.ajax({
						url: baseUrl+"/ledger/del",
						type: "POST",
						data:{"id":data.id},
						dataType: "json",
						success: function(data){
							if(data.code==200){
								obj.del();
								layer.close(index);
								layer.msg("删除成功", {icon: 6});
							}else{
								layer.msg("删除失败", {icon: 5});
							}
						}
					});
				});
			} else if(obj.event === 'edit'){
				layer.confirm('确定修改吗？', function(index){
					location.href="./accountsEdit.html?pos=0&childPos=0&id="+data.id;
				});
			}
		});
	});
}())